package com.miracle.java2110.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.miracle.java2110.common.PageResult;
import com.miracle.java2110.common.R;
import com.miracle.java2110.mapper.DrinksMapper;
import com.miracle.java2110.mapper.GuestMapper;
import com.miracle.java2110.mapper.OrderMapper;
import com.miracle.java2110.mapper.RoomManagerMapper;
import com.miracle.java2110.pojo.*;
import com.miracle.java2110.service.OrderService;
import com.miracle.java2110.vo.OrderAddVo;
import com.miracle.java2110.vo.OrderTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.print.ServiceDialog;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("orderService")
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private GuestMapper guestMapper;

    @Override
    public PageResult findOrders(Map<String, Object> params) {
        int page = Integer.parseInt(params.get("page") + "");
        int limit = Integer.parseInt(params.get("limit") + "");

        PageHelper.startPage(page, limit);

        List<Order> orders = orderMapper.selectByParams(params);

        PageInfo<Order> userPageInfo = new PageInfo<>(orders);
        PageResult pageResult = new PageResult();

        pageResult.setCode(0);
        pageResult.setMsg("查询用户分页数据");
        pageResult.setCount(userPageInfo.getTotal());
        pageResult.setData(orders);

        return pageResult;
    }

    @Override
    public R deleteById(String[] ids) {
        for (String s : ids) {
            int id = Integer.parseInt(s);
            orderMapper.deleteOrderDrinks(id);
            orderMapper.deleteOrderEmployee(id);
            orderMapper.deleteOrderService(id);
            orderMapper.deleteOrders(id);
        }


        return R.ok(200, "删除成功");
    }

    @Override
    public List<OrderTreeVo> orderDeteilById(Integer id) {
        Order order = orderMapper.selectOrderById(id);

        // 一级标题
        ArrayList<OrderTreeVo> treeVoList = new ArrayList<>();

        // 订单节点
        treeVoList.add(new OrderTreeVo("订单号：" + order.getId()));

        // 客户节点
        OrderTreeVo guestTree = new OrderTreeVo("客户名：" + order.getGuest().getGname());
        List<OrderTreeVo> guestList = new ArrayList<>();
        guestList.add(new OrderTreeVo("客户手机号：" + order.getGuest().getGphone()));
        guestTree.setChildren(guestList);
        treeVoList.add(guestTree);

        // 房间节点
        OrderTreeVo roomTree = new OrderTreeVo("房间号：" + order.getRoomId());
        List<OrderTreeVo> roomList = new ArrayList<>();
        roomList.add(new OrderTreeVo("房间类型：" + order.getRoom().getRoomType()));
        roomList.add(new OrderTreeVo("房间价格：" + order.getRoom().getRoomPrice()));
        roomTree.setChildren(roomList);
        treeVoList.add(roomTree);

        // 服务节点
        OrderTreeVo serviceTree = new OrderTreeVo("服务项目");
        List<OrderTreeVo> serviceList = new ArrayList<>();
        for (com.miracle.java2110.pojo.Serviced service : order.getServiceList()) {
            serviceList.add(new OrderTreeVo(service.getSname() + "：" + service.getSprice() + "元"));
        }
        serviceTree.setChildren(serviceList);
        treeVoList.add(serviceTree);

        // 技师节点
        OrderTreeVo employeeTree = new OrderTreeVo("服务技师");
        List<OrderTreeVo> employeeList = new ArrayList<>();
        for (Employeemanager employee : order.getEmployeeList()) {
            employeeList.add(new OrderTreeVo(employee.getId() + "号技师：" + employee.getEmployname()));
        }
        employeeTree.setChildren(employeeList);
        treeVoList.add(employeeTree);

        // 酒水节点
        OrderTreeVo drinksTree = new OrderTreeVo("已点酒水");
        List<OrderTreeVo> drinksList = new ArrayList<>();
        for (Drinks drinks : order.getDrinksList()) {
            drinksList.add(new OrderTreeVo(drinks.getDtype() + "：" + drinks.getDprice() + "元  数量 ：" + drinks.getCount() + "瓶"));
        }
        drinksTree.setChildren(drinksList);
        treeVoList.add(drinksTree);

        treeVoList.add(new OrderTreeVo("消费金额：" + order.getOrderMoney() + "元"));

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        treeVoList.add(new OrderTreeVo("下单时间：" + formatter.format(order.getOrderTime())));

        return treeVoList;
    }

    @Override
    public R addOrder(OrderAddVo orderAddVo) {
        Integer guestId = orderMapper.selectGuestId(orderAddVo.getGuestPhone());

        if (guestId == null) {
            Guest guest = new Guest();
            guest.setGname(orderAddVo.getGuestName());
            guest.setGphone(orderAddVo.getGuestPhone());

            guestMapper.insertSelective(guest);

            guestId = orderMapper.selectGuestId(orderAddVo.getGuestPhone());
        }

        // 减少酒水库存
        orderMapper.updateDrinks(1, orderAddVo.getD1());
        orderMapper.updateDrinks(2, orderAddVo.getD2());
        orderMapper.updateDrinks(3, orderAddVo.getD3());
        orderMapper.updateDrinks(4, orderAddVo.getD4());
        orderMapper.updateDrinks(6, orderAddVo.getD6());
        orderMapper.updateDrinks(7, orderAddVo.getD7());
        orderMapper.updateDrinks(8, orderAddVo.getD8());

        // 加酒水消费
        Integer orderMoney = orderAddVo.getD1() * 99 + orderAddVo.getD2() * 16 + orderAddVo.getD3() * 16 + orderAddVo.getD4() * 66 +
                orderAddVo.getD6() * 55 + orderAddVo.getD7() * 55 + orderAddVo.getD8() * 66;

        // 加服务消费
        if (orderAddVo.getServiceType() != null) {
            for (String serviceId : orderAddVo.getServiceType()) {
                if (serviceId != null) {
                    orderMoney += orderMapper.selectServicePrice(Integer.parseInt(serviceId));
                }
            }
        }


        List<Integer> roomId = orderMapper.selectRoomId(orderAddVo.getRoomType(), "可租");

        // 加房间消费
        orderMoney += orderMapper.selectRoomPrice(roomId.get(0));

        if (roomId.size() == 0) {
            return R.error(400, "该类型房间已无");
        } else {
            Order order = new Order(guestId, roomId.get(0), orderMoney, new Date());

            int cows = orderMapper.addOrder(order);

            if (cows == 1) {
                orderMapper.updateRoom(roomId.get(0), "已租");
                if (orderAddVo.getServiceType() != null) {
                    for (String serviceId : orderAddVo.getServiceType()) {
                        if (serviceId != null) {
                            orderMapper.addOrderService(order.getId(), Integer.parseInt(serviceId));
                            orderMapper.addOrderEmployee(order.getId(), Integer.parseInt(serviceId));
                        }
                    }
                }

                if (orderAddVo.getD1() != 0) {
                    orderMapper.addOrderdrinks(order.getId(), 1, orderAddVo.getD1());
                }
                if (orderAddVo.getD2() != 0) {
                    orderMapper.addOrderdrinks(order.getId(), 2, orderAddVo.getD2());
                }
                if (orderAddVo.getD3() != 0) {
                    orderMapper.addOrderdrinks(order.getId(), 3, orderAddVo.getD3());
                }
                if (orderAddVo.getD4() != 0) {
                    orderMapper.addOrderdrinks(order.getId(), 4, orderAddVo.getD4());
                }
                if (orderAddVo.getD6() != 0) {
                    orderMapper.addOrderdrinks(order.getId(), 6, orderAddVo.getD6());
                }
                if (orderAddVo.getD7() != 0) {
                    orderMapper.addOrderdrinks(order.getId(), 7, orderAddVo.getD7());
                }
                if (orderAddVo.getD8() != 0) {
                    orderMapper.addOrderdrinks(order.getId(), 8, orderAddVo.getD8());
                }
                return R.ok(200, "添加订单成功");
            } else {
                return R.error(400, "添加订单失败");
            }
        }

    }

    @Override
    public R uploadExcel(List<OrderAddVo> list) {
        for (OrderAddVo orderAddVo : list) {
            addOrder(orderAddVo);
        }

        return R.ok(200, "导入成功");
    }

    @Override
    public OrderAddVo getEdit(Integer id) {
        Order order = orderMapper.selectOrderById(id);
        OrderAddVo orderAddVo = new OrderAddVo();
        orderAddVo.setGuestName(order.getGuest().getGname());
        orderAddVo.setGuestPhone(order.getGuest().getGphone());
        orderAddVo.setRoomType(order.getRoom().getRoomType());
        List<Serviced> serviceList = order.getServiceList();
        ArrayList<String> list = new ArrayList<>();
        for (Serviced serviced : serviceList) {
            list.add(serviced.getId().toString());
        }
        String[] serviceArray = new String[list.size()];
        orderAddVo.setServiceType(list.toArray(serviceArray));

        for (Drinks drinks : order.getDrinksList()) {
            try {
                Method method = orderAddVo.getClass().getMethod("setD" + drinks.getDid(), Integer.class);
                method.setAccessible(true);
                method.invoke(orderAddVo, drinks.getCount());
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        return orderAddVo;
    }

    @Override
    public R orderUpdate(OrderAddVo newOrder) {
        Integer orderId = newOrder.getOrderId();
        OrderAddVo oldOrder = getEdit(orderId);
        Integer orderMoney = 0;
        if (!newOrder.getRoomType().equals(oldOrder.getRoomType())) {
            orderMapper.updateRoom(oldOrder.getRoomId(), "可租");
            List<Integer> newRoomId = orderMapper.selectRoomId(newOrder.getRoomType(), "可租");
            orderMapper.updateOrderRoomId(orderId, newRoomId.get(0));
            orderMapper.updateRoom(newRoomId.get(0), "已租");
            orderMoney += orderMapper.selectRoomPrice(newRoomId.get(0));
        } else {
            orderMoney += orderMapper.selectRoomPrice(newOrder.getRoomId());
        }

        // 比较服务项目
        ArrayList<String> list = new ArrayList<>();
        if (newOrder.getServiceType() != null) {
            for (String s : newOrder.getServiceType()) {
                if (s != null) {
                    list.add(s);
                }
            }
            String[] newServiceType = new String[list.size()];
            newServiceType = list.toArray(newServiceType);
            if (!Arrays.equals(oldOrder.getServiceType(), newServiceType)) {
                orderMapper.deleteOrderService(orderId);
                orderMapper.deleteOrderEmployee(orderId);

                for (String serviceId : newServiceType) {
                    orderMapper.addOrderService(orderId, Integer.parseInt(serviceId));
                    orderMapper.addOrderEmployee(orderId, Integer.parseInt(serviceId));
                    orderMoney += orderMapper.selectServicePrice(Integer.parseInt(serviceId));
                }
            }
        } else {
            orderMapper.deleteOrderService(orderId);
            orderMapper.deleteOrderEmployee(orderId);
        }



        // 比较酒水
        if (!newOrder.getD1().equals(oldOrder.getD1())) {
            int difference = newOrder.getD1() - oldOrder.getD1();
            if (oldOrder.getD1() == 0) {
                orderMapper.addOrderdrinks(orderId, 1, newOrder.getD1());
            } else if (newOrder.getD1() == 0) {
                orderMapper.deleteOrderDrinksByDrinkId(orderId, 1);
            } else {
                orderMapper.updateOrderdrinks(orderId, 1, difference);
                orderMapper.updateDrinks(1, -difference);
            }
        }
        if (!newOrder.getD2().equals(oldOrder.getD2())) {
            int difference = newOrder.getD2() - oldOrder.getD2();
            if (oldOrder.getD2() == 0) {
                orderMapper.addOrderdrinks(orderId, 2, newOrder.getD2());
            } else if (newOrder.getD2() == 0) {
                orderMapper.deleteOrderDrinksByDrinkId(orderId, 2);
            } else {
                orderMapper.updateOrderdrinks(orderId, 2, difference);
                orderMapper.updateDrinks(2, -difference);
            }
        }
        if (!newOrder.getD3().equals(oldOrder.getD3())) {
            int difference = newOrder.getD3() - oldOrder.getD3();
            if (oldOrder.getD3() == 0) {
                orderMapper.addOrderdrinks(orderId, 3, newOrder.getD3());
            } else if (newOrder.getD3() == 0) {
                orderMapper.deleteOrderDrinksByDrinkId(orderId, 3);
            } else {
                orderMapper.updateOrderdrinks(orderId, 3, difference);
                orderMapper.updateDrinks(3, -difference);
            }
        }
        if (!newOrder.getD4().equals(oldOrder.getD4())) {
            int difference = newOrder.getD4() - oldOrder.getD4();
            if (oldOrder.getD4() == 0) {
                orderMapper.addOrderdrinks(orderId, 4, newOrder.getD4());
            } else if (newOrder.getD4() == 0) {
                orderMapper.deleteOrderDrinksByDrinkId(orderId, 4);
            } else {
                orderMapper.updateOrderdrinks(orderId, 4, difference);
                orderMapper.updateDrinks(4, -difference);
            }
        }
        if (!newOrder.getD6().equals(oldOrder.getD6())) {
            int difference = newOrder.getD6() - oldOrder.getD6();
            if (oldOrder.getD6() == 0) {
                orderMapper.addOrderdrinks(orderId, 6, newOrder.getD6());
            } else if (newOrder.getD6() == 0) {
                orderMapper.deleteOrderDrinksByDrinkId(orderId, 6);
            } else {
                orderMapper.updateOrderdrinks(orderId, 6, difference);
                orderMapper.updateDrinks(6, -difference);
            }
        }
        if (!newOrder.getD7().equals(oldOrder.getD7())) {
            int difference = newOrder.getD7() - oldOrder.getD7();
            if (oldOrder.getD7() == 0) {
                orderMapper.addOrderdrinks(orderId, 7, newOrder.getD7());
            } else if (newOrder.getD7() == 0) {
                orderMapper.deleteOrderDrinksByDrinkId(orderId, 7);
            } else {
                orderMapper.updateOrderdrinks(orderId, 7, difference);
                orderMapper.updateDrinks(7, -difference);
            }
        }
        if (!newOrder.getD8().equals(oldOrder.getD8())) {
            int difference = newOrder.getD8() - oldOrder.getD8();
            if (oldOrder.getD8() == 0) {
                orderMapper.addOrderdrinks(orderId, 8, newOrder.getD8());
            } else if (newOrder.getD8() == 0) {
                orderMapper.deleteOrderDrinksByDrinkId(orderId, 8);
            } else {
                orderMapper.updateOrderdrinks(orderId, 8, difference);
                orderMapper.updateDrinks(8, -difference);
            }
        }

        orderMoney += newOrder.getD1() * 99 + newOrder.getD2() * 16 + newOrder.getD3() * 16 + newOrder.getD4() * 66 +
                newOrder.getD6() * 55 + newOrder.getD7() * 55 + newOrder.getD8() * 66;

        orderMapper.updateOrderMoney(orderId, orderMoney);

        return R.ok(200, "修改成功");
    }
}
